package com.gomore.opple.module.cards.used;

import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.bean.WrapperResponseEntity;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.common.DataPage;
import com.gomore.opple.rest.jdecard.GroupByOrderNumber;
import com.gomore.opple.rest.jdecard.RsJingDongECardRequest;
import com.gomore.opple.rest.jdecard.RsJingDongECardResponse;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.jdecard.entity.TOJingdongEcardEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class UsedPresenter implements UsedContract.Presenter {

    private DataRepository mDataRepositroy;
    private final UsedContract.View mView;
    private CompositeSubscription mSubscriptions;
    private List<GroupByOrderNumber> groupByOrderNumbers = new ArrayList<>();
    private int rows = GlobalConstant.PAGE_SIZE;
    private int page = GlobalConstant.PAGE_START_ZERO;

    @Inject
    UsedPresenter(DataRepository dataRepository, UsedContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void queryJDE(final boolean isLoadMore) {
        if (isLoadMore) {
            page++;
        } else {
            groupByOrderNumbers.clear();
            page = GlobalConstant.PAGE_START_ZERO;
        }
        mView.showProgressDialog();
        RsJingDongECardRequest rsJingDongECardRequest = new RsJingDongECardRequest();
        String employeeId = mDataRepositroy.getUser().getEmployee().getId();
        rsJingDongECardRequest.setGuideIdEquals(employeeId);
        rsJingDongECardRequest.setStateEquals(GlobalConstant.JDEState.USED);
        DataPage dataPage = new DataPage();
        dataPage.setRows(rows);
        dataPage.setPage(page);
        rsJingDongECardRequest.setPage(dataPage);
        mSubscriptions.clear();
        Subscription subscription = mDataRepositroy.queryJDE(rsJingDongECardRequest)
                .map(new ServerResponseFunc<RsJingDongECardResponse>())
                .onErrorResumeNext(new HttpResponseFunc<RsJingDongECardResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<RsJingDongECardResponse>() {
                    @Override
                    public void onNext(RsJingDongECardResponse rsJingDongECardResponse) {
                        super.onNext(rsJingDongECardResponse);
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                        groupByOrderNumbers.addAll(rsJingDongECardResponse.getList());
                        mView.showContentView();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<GroupByOrderNumber> getData() {
        return groupByOrderNumbers;
    }
}
